package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("StoremethodController")
public class StoremethodController extends CmiEmaController {

	@RequestMapping("/listStoremethods")
	public ModelAndView listStoremethods(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = storemethodPaginationService.updatePagination(request, pagination);
		Set<Storemethod> storemethods = storemethodPaginationService.getPaginationStoremethods(pagination);

		mav.addObject("storemethods", storemethods);
		mav.addObject("entityName", "storemethod");
		mav.addObject("pagination", pagination);
		mav.setViewName("storemethod/listStoremethods.jsp");
		return mav;
	}

	public String listStoremethods() {
		return "redirect:/listStoremethods";
	}

	@RequestMapping("/viewStoremethod")
	public ModelAndView viewStoremethod(@RequestParam Integer storemethodId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("storemethod", storemethodDAO.findStoremethodByPrimaryKey(storemethodId));
		mav.setViewName("storemethod/viewStoremethod.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteStoremethods")
	public ModelAndView confirmDeleteStoremethods(
			@RequestParam(value = "deleteStoremethodsIds", required = true) Set<Integer> deleteStoremethodsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Storemethod> deleteStoremethods = storemethodDAO.findStoremethodsByIds(deleteStoremethodsIds);
		Set<Storemethod> referencedStoremethods = storemethodService.loadReferenceStoremethods(deleteStoremethods);
		boolean hasReferenced;
		if (referencedStoremethods.size() == 0) {
			mav.addObject("deleteEntitys", deleteStoremethods);
			hasReferenced = false;
		} else {
			deleteStoremethods.removeAll(referencedStoremethods);
			mav.addObject("referencedEntitys", referencedStoremethods);
			mav.addObject("deleteEntitys", deleteStoremethods);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Storemethod");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listStoremethods");
		return mav;
	}

	@RequestMapping("/deleteStoremethod")
	public String deleteStoremethod(@RequestParam Integer storemethodId) {
		Storemethod storemethod = storemethodDAO.findStoremethodByPrimaryKey(storemethodId);
		storemethodService.deleteStoremethod(storemethod);
		return "forward:/listStoremethods";
	}

	@RequestMapping("/confirmDeleteStoremethod")
	public ModelAndView confirmDeleteStoremethod(@RequestParam Integer storemethodId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("storemethod", storemethodDAO.findStoremethodByPrimaryKey(storemethodId));

		mav.setViewName("storemethod/deleteStoremethod.jsp");

		return mav;
	}

	@RequestMapping("/deleteStoremethods")
	public String deleteStoremethods(
			@RequestParam(value = "deleteStoremethodsId", required = true) Set<Integer> deleteStoremethodsId) {
		storemethodService.deleteStoremethods(deleteStoremethodsId);
		return "forward:/listStoremethods";
	}

	@RequestMapping("/editStoremethod")
	public ModelAndView editStoremethod(@RequestParam Integer storemethodId) {

		ModelAndView mav = new ModelAndView();

		Storemethod storemethod = storemethodDAO.findStoremethodByPrimaryKey(storemethodId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("storemethod", storemethod);

		// 把所有的引用storemethod对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(storemethodService.loadStoremethodObjectReferencedSet(storemethod));

		// 将编辑页面storemethod添加到mav中
		mav.setViewName("storemethod/editStoremethod.jsp");
		return mav;
	}

	@RequestMapping("/newStoremethod")
	public ModelAndView newStoremethod() {
		ModelAndView mav = new ModelAndView();
		Storemethod storemethod = new Storemethod();

		mav.addObject("storemethod", storemethod);

		// 把所有的引用storemethod对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(storemethodService.loadStoremethodObjectReferencedSet(storemethod));

		mav.addObject("newFlag", true);
		mav.setViewName("storemethod/editStoremethod.jsp");

		return mav;
	}

	@RequestMapping("/saveStoremethod")
	public ModelAndView saveStoremethod(@ModelAttribute Storemethod storemethod) {

		ModelAndView mav = new ModelAndView();

		Storemethod existingStoremethod = CmiHibernateProxyUtil
				.deproxy(storemethodDAO.findStoremethodByPhysicalPrimaryKey(storemethod.getName()));

		// 新建记录
		if (storemethod.getId() == null) {
			if (storemethod.equals(existingStoremethod)) {

				mav.addAllObjects(storemethodService.loadStoremethodClassReferencedSet(storemethod));
				// mav = editContinue(mav, storemethod);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("storemethod/editStoremethod.jsp");
			} else {
				storemethodService.saveStoremethod(storemethod);
				mav.setViewName("forward:/listStoremethods");
			}
		}
		// 编辑记录
		else {
			Storemethod originStoremethod = CmiHibernateProxyUtil
					.deproxy(storemethodDAO.findStoremethodByPrimaryKey(storemethod.getId()));
			if (!storemethod.equals(originStoremethod) && storemethod.equals(existingStoremethod)) {
				mav.addAllObjects(storemethodService.loadStoremethodClassReferencedSet(storemethod));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("storemethod/editStoremethod.jsp");
			} else {
				storemethodService.saveStoremethod(storemethod);
				mav.setViewName("forward:/listStoremethods");
			}
		}
		return mav;
	}

	@RequestMapping("/storemethodController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
